<?php

defined('SYSPATH') or die('No direct access allowed.');

/**
 * Service Session_User
 */
class Model_Service_Session_User extends Domain_Dao_Session_User {

    public $_rep;

    /**
     * 
     */
    public function __construct() {
        $o_repository_manager = new Domain_Repositories_RepositoryManager;
        $this->_rep = $o_repository_manager->getSessionUserRepository();
    }

    /**
     *
     * @return <type>
     */
    public function load($id) {
        return $this->_rep->load($id);
    }

    /**
     *
     * @param <type> $offset
     * @param <type> $limit
     * @param <type> $filter
     * @param <type> $sort
     * @param <type> $count
     * @return <type>
     */
    public function load_all($is_count = false, $offset = null, $limit = null, $filter = null, $sort = null) {
        return $this->_rep->load_all($offset, $limit, $filter, $sort, $is_count);
    }

    /**
     *
     * @param Domain_Entities_Entity $o_entity
     * @return <type> 
     */
    public function insert($o_entity) {
        return $this->_rep->insert_by($o_entity);
    }

    /**
     *
     * @param Domain_Entities_Entity $o_entity
     * @return <type>
     */
    public function update($o_entity) {
        return $this->_rep->update_by($o_entity);
    }

    /**
     *
     * @param <type> $id
     * @return <type> 
     */
    public function delete($id) {
        return $this->_rep->delete_by($id);
    }

    /**
     *
     * @param <type> $user_id
     * @return <type> 
     */
    public function get_by_user_id($user_id, $type = 'DESKTOP') {
        $this->_rep->clear();
        $this->_rep->builder_select('user_id', array('where' => '='), $user_id);
        $this->_rep->builder_select('type', array('where' => '='), $type);
        return $this->_rep->query();
    }

    /**
     *
     * @param <type> $user_id
     * @return <type> 
     */
    public function delete_by_user($user_id, $type = 'DESKTOP') {
        $o_su = $this->get_by_user_id($user_id, $type);
        if (empty($o_su)) {
            return false;
        }
        $o_entity_us = $o_su->get_data();
        if ($o_entity_us->getId() <= 0) {
            return false;
        }
        return $this->delete($o_entity_us->getId());
    }
    
    public function get_by_session_key($session_key){
        $this->_rep->clear();
        $this->_rep->builder_select('session_key', array('where'=>'='),$session_key);
        $this->_rep->builder_select('type', array('where'=>'='),'MOBILE');
        return $this->_rep->query();
    }

}

?>
